package com.yc.bank.biz;

import com.yc.bank.dao.AccountDao;
import com.yc.bank.dao.OprecorDao;
import com.yc.bank.entity.Account;
import com.yc.bank.entity.Oprecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.awt.geom.Dimension2D;

@Service
@Transactional
public class AccountBiz {

    @Autowired
    AccountDao adao;

    @Autowired
    OprecorDao odao;

    //存款
    @Transactional
    public void deposit(String id , double money){
        adao.updateBalance(id, money);
        Oprecord oprecord = new Oprecord();
        oprecord.setAid(id);
        //int i = 1 / 0;
        oprecord.setOpmoney(money);
        odao.insert(oprecord);
    }

    //取款
    public void withdraw(String id , double money) throws BizExcepion{
        Account account = adao.selectByid(id);
        if (account.getBanlance() < money){
            throw new BizExcepion("账号余额不足");
        }
        adao.updateBalance(id, money);
        Oprecord oprecord = new Oprecord();
        oprecord.setAid(id);
        oprecord.setOpmoney(-money);
        odao.insert(oprecord);
    }

    //转账
    //noRollbackFor  不会回滚异常 ==》类型 ：运行时异常
    @Transactional(rollbackFor = {BizExcepion.class})
    public void transform(String id1, String id2 , double money) throws BizExcepion {
        deposit(id1, money);
        withdraw(id2, money);
    }
}
